package com.audaque.springboot.foshanupload.web.config;


import com.audaque.springboot.foshanupload.core.constants.ComConst;
import com.audaque.springboot.foshanupload.web.filter.BodyConvertFilter;
import com.audaque.springboot.foshanupload.web.filter.JspFilter;
import com.audaque.springboot.foshanupload.web.filter.DoFilterExceptionFilter;
import com.audaque.springboot.foshanupload.web.filter.ParameterConvertFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author zgb
 * @desc ...
 * @date 2022-08-11 01:09:07
 */
@Configuration
public class FilterConfig {
/*
    @Autowired
    private BodyConvertFilter bodyConvertFilter;

    @Autowired
    private ParameterConvertFilter parameterConvertFilter;
*/


    @Autowired
    private DoFilterExceptionFilter doFilterExceptionFilter;

    @Autowired
    private JspFilter jspFilter;


    /**
     *   //跨域:方式2
     * 允许跨域调用的过滤器
     */
/*
    @Bean
    public CorsFilter corsFilter() {
        //1.添加CORS配置信息
        CorsConfiguration config = new CorsConfiguration();
        //1) 允许的域,不要写*，否则cookie就无法使用了
//    config.addAllowedOrigin("http://localhost:8080");
        config.addAllowedOrigin("*");
        //2) 是否发送Cookie信息
        config.setAllowCredentials(true);
        //3) 允许的请求方式
        config.addAllowedMethod("OPTIONS");
        config.addAllowedMethod("HEAD");
        config.addAllowedMethod("GET");
        config.addAllowedMethod("PUT");
        config.addAllowedMethod("POST");
        config.addAllowedMethod("DELETE");
        config.addAllowedMethod("PATCH");
        // 4）允许的头信息
        config.addAllowedHeader("*");
        //2.添加映射路径，我们拦截一切请求
        UrlBasedCorsConfigurationSource configSource = new
                UrlBasedCorsConfigurationSource();
        configSource.registerCorsConfiguration("/**", config);
        //3.返回新的CorsFilter.
        return new CorsFilter(configSource);
    }
*/
/*
    *//**
     * 配置过滤器
     * order属性:控制过滤器加载顺序：数字越小，加载越早
     *
     * @return
     *//*
    @Bean
    public FilterRegistrationBean bodyConvertFilterRegistrationBean() {
        //新建过滤器注册类
        FilterRegistrationBean registration = new FilterRegistrationBean();
        // 添加我们写好的过滤器
        registration.setFilter(bodyConvertFilter);
        registration.setName("bodyConvertFilter");
        // 设置过滤器的URL模式
        registration.addUrlPatterns(ComConst.FILTER_ANY_MATCH);
        //控制过滤器加载顺序：数字越小，加载越早
        registration.setOrder(Integer.MAX_VALUE - 10);
        return registration;
    }

    @Bean
    public FilterRegistrationBean parameterConvertFilterRegistrationBean() {
        //新建过滤器注册类
        FilterRegistrationBean registration = new FilterRegistrationBean();
        // 添加我们写好的过滤器
        registration.setFilter(parameterConvertFilter);
        registration.setName("parameterConvertFilter");
        // 设置过滤器的URL模式
        registration.addUrlPatterns(ComConst.FILTER_ANY_MATCH);
        //控制过滤器加载顺序：数字越小，加载越早
        registration.setOrder(Integer.MAX_VALUE - 10);
        return registration;
    }*/

    @Bean
    public FilterRegistrationBean jspFilterRegistrationBean() {
        //新建过滤器注册类
        FilterRegistrationBean registration = new FilterRegistrationBean();
        // 添加我们写好的过滤器
        registration.setFilter(jspFilter);
        registration.setName("jspFilter");
        // 设置过滤器的URL模式
        registration.addUrlPatterns(ComConst.FILTER_ANY_MATCH);
        //控制过滤器加载顺序：数字越小，加载越早
        registration.setOrder(Integer.MAX_VALUE - 11);
        return registration;
    }




    @Bean
    public FilterRegistrationBean doFilterExceptionFilterRegistrationBean() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(doFilterExceptionFilter);
        registration.setName("doFilterExceptionFilter");
        //此处尽量小，要比其他Filter靠前
        registration.setOrder(-1);
        return registration;
    }





}
